﻿using System;
using System.Windows.Media;

namespace MyFloridaHouse.libSvUtilities.CustomColors
{
    public class RGB
    {

        public static Color GetColorFromHex(double opacity, string color)
        {

            // from http://forums.silverlight.net/forums/p/24190/86829.aspx
            // deal with opacity

            if (opacity > 1.0)
                opacity = 1.0;

            if (opacity < 0.0)
                opacity = 0.0;

            // get the hex value of the alpha chanel (opacity):
            byte a = (byte)(Convert.ToInt32(255 * opacity));

            try
            {
                byte r = (byte)(Convert.ToUInt32(color.Substring(0, 2), 16));
                byte g = (byte)(Convert.ToUInt32(color.Substring(2, 2), 16));
                byte b = (byte)(Convert.ToUInt32(color.Substring(4, 2), 16));

                return (Color.FromArgb(a, r, g, b));
            }
            catch (Exception ex)
            {
                // pick a fugly color, but don't cause the system to barf.
                return (Color.FromArgb(255, 255, 0, 0));
            }
        }


        public static Color GetColorFromPercent(byte alpha, double pct)
        {
            // algorith came from http://stackoverflow.com/questions/340209/generate-colors-between-red-and-green-for-a-power-meter

            try
            {
                byte r = 0;
                byte g = 0;
                byte b = 0;

                byte percent = 0;
                if (pct < 0)
                {
                    percent = 0;
                }
                else if (pct > 100)
                {
                    percent = 100;
                }
                else
                {
                    percent = (byte)(Convert.ToUInt32(pct));
                }

                r = (byte)((255 * percent) / 100);
                g = 0;
                b = (byte)((255 * (100 - percent)) / 100);


                return Color.FromArgb(alpha, r, g, b);
            }
            catch
            {
                return Colors.Green;
            }
        }
    }
}
